home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / ifcico / nlindex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-09  |  3.6 KB  |  191 lines

  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6. #include <dirent.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #ifdef HAS_NDBM_H
  10. #include <ndbm.h>
  11. #else
  12. #include <dbm.h>
  13. #endif
  14. #include "xutil.h"
  15. #include "lutil.h"
  16. #include "ftn.h"
  17. #include "config.h"
  18. #include "nodelist.h"
  19. #include "nlindex.h"
  20.  
  21. #ifdef HAS_NDBM_H
  22. DBM *nldb=NULL;
  23. #endif
  24. int openstatus = 0;
  25. int n;
  26.  
  27. DIR *dp;
  28. struct dirent *de;
  29.  
  30. struct _nodelist *nodevector;
  31.  
  32. struct _pkey pkey[] = {
  33.     {"Down",NODE,DOWN},
  34.     {"Hold",NODE,HOLD},
  35.     {"Host",NET,0},
  36.     {"Hub",NODE,HUB},
  37.     {"Point",POINT,0},
  38.     {"Pvt",NODE,PVT},
  39.     {"Region",NET,REGION},
  40.     {"Zone",ZONE,0},
  41.     {NULL,0,0}
  42. };
  43.  
  44. struct _fkey fkey[] = {
  45.     {"CM",CM},
  46.     {"MO",MO},
  47.     {"LO",LO},
  48.     {"V21",V21},
  49.     {"V22",V22},
  50.     {"V29",V29},
  51.     {"V32",V32},
  52.     {"V32B",V32B|V32},
  53.     {"V33",V33},
  54.     {"V34",V34},
  55.     {"V42",V42|MNP},
  56.     {"V42B",V42B|V42|MNP},
  57.     {"MNP",MNP},
  58.     {"H96",H96},
  59.     {"HST",HST|MNP},
  60.     {"H14",H14|HST|MNP},
  61.     {"H16",H16|H14|HST|MNP|V42B},
  62.     {"MAX",MAX},
  63.     {"PEP",PEP},
  64.     {"CSP",CSP},
  65.     {"ZYX",ZYX|V32B|V32|V42B|V42|MNP},
  66.     {"MN",MN},
  67.     {"XA",XA},
  68.     {"XB",XB},
  69.     {"XC",XC},
  70.     {"XP",XP},
  71.     {"XR",XR},
  72.     {"XW",XW},
  73.     {"XX",XX},
  74.     {NULL,0}
  75. };
  76.  
  77. int initnl(void)
  78. {
  79.     int i,rc;
  80.     fa_list *tmp;
  81.     time_t lastmtime;
  82.     struct stat stbuf;
  83.     char *indexnm,*nlnm=NULL,*p;
  84.  
  85.     if (openstatus > 1) return openstatus;
  86.     if (openstatus == 1) return 0;
  87.  
  88.     n=0;
  89.     for (tmp=nllist;tmp;tmp=tmp->next) n++;
  90.     debug(20,"Initialize %d nodelists",n);
  91.  
  92.     nodevector=(struct _nodelist *)xmalloc(n * sizeof(struct _nodelist));
  93.  
  94.     lastmtime=configtime;
  95.     i=0;
  96.     for (tmp=nllist;tmp;tmp=tmp->next)
  97.     {
  98.         if (tmp->addr->domain)
  99.             nodevector[i].domain=tmp->addr->domain;
  100.         else if (whoami->addr->domain)
  101.             nodevector[i].domain=whoami->addr->domain;
  102.         else
  103.             nodevector[i].domain="fidonet";
  104.  
  105.         nlnm=xstrcpy(tmp->addr->name);
  106.         if ((rc=stat(tmp->addr->name,&stbuf)) != 0)
  107.         {
  108.             int next,mext=0;
  109.  
  110.             if (nlnm) free(nlnm);
  111.             if ((nlnm=strrchr(tmp->addr->name,'/')))
  112.                 nlnm++;
  113.             else nlnm=tmp->addr->name;
  114.             if (dp == NULL) dp=opendir(nlbase);
  115.             if (dp != NULL)
  116.             {
  117.                 rewinddir(dp);
  118.                 while ((de=readdir(dp)))
  119.                 if (strncmp(de->d_name,nlnm,strlen(nlnm)) == 0)
  120.                 {
  121.                     p=(de->d_name)+strlen(nlnm);
  122.                     if ((*p == '.') && (strlen(p) == 4) &&
  123.                         (strspn(p+1,"0123456789") == 3))
  124.                     {
  125.                         next=atoi(p+1);
  126.                         if (next > mext) mext=next;
  127.                     }
  128.                 }
  129.             }
  130.             else logerr("$cannot open \"%s\" directory",nlbase);
  131.             nlnm=xstrcpy(tmp->addr->name);
  132.             nlnm=xstrcat(nlnm,".999");
  133.             sprintf(nlnm+strlen(nlnm)-3,"%03d",mext);
  134.             debug(20,"try \"%s\" nodelist",nlnm);
  135.             rc=stat(nlnm,&stbuf);
  136.         }
  137.         if (rc == 0)
  138.         {
  139.             if (stbuf.st_mtime > lastmtime) 
  140.                 lastmtime=stbuf.st_mtime;
  141.             nodevector[i].fp=fopen(nlnm,"r");
  142.             if (nodevector[i].fp == NULL)
  143.                 logerr("$cannot open nodelist \"%s\"",nlnm);
  144.             else debug(20,"opened nodelist \"%s\"",nlnm);
  145.         }
  146.         else
  147.         {
  148.             logerr("$cannot stat nodelist \"%s\"",tmp->addr->name);
  149.             nodevector[i].fp=NULL;
  150.         }
  151.         i++;
  152.         if (nlnm) free(nlnm);
  153.     }
  154.  
  155.     if (dp != NULL) closedir(dp);
  156.     dp=NULL;
  157.  
  158.     for (i=0;i<n;i++)
  159.     {
  160.         debug(20,"nodelist: %3d: %-08s %08lx",
  161.             i,nodevector[i].domain,nodevector[i].fp);
  162.     }
  163.  
  164.     indexnm=xstrcpy(nlbase);
  165.     indexnm=xstrcat(indexnm,INDEX);
  166.     indexnm=xstrcat(indexnm,".dir");
  167.     if ((stat(indexnm,&stbuf) != 0) || (stbuf.st_mtime < lastmtime)) rc=1;
  168.     else rc=0;
  169.  
  170.     indexnm[strlen(indexnm)-4]='\0';
  171.     debug(20,"Trying open existing \"%s\"",indexnm);
  172. #ifdef HAS_NDBM_H
  173.     if (nldb == NULL)
  174.     if ((nldb=dbm_open(indexnm,O_RDONLY,0600)) == NULL)
  175.         rc=2;
  176. #else
  177.     if (dbminit(indexnm) != 0) rc=2;
  178. #endif
  179.  
  180.     free(indexnm);
  181.  
  182.     if (rc > 1)
  183.     {
  184.         openstatus=2;
  185.         logerr("$cannot open nodelist index (%d)",rc);
  186.     }
  187.     else openstatus=1;
  188.  
  189.     return rc;
  190. }
  191.